package com.ta.stage.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ta.stage.dao.MenuDao;
import com.ta.stage.entity.Keyword;
import com.ta.stage.entity.Menu;
import com.ta.stage.entity.vo.MenuVo;
import com.ta.stage.result.Result;
import com.ta.stage.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu/")
@Api(tags={"权限 MenuController"})
public class MenuController {

    @Autowired
    private MenuService menuService;

    @ApiOperation(value = "查询权限")
    @RequestMapping(value = "findMenu", method = RequestMethod.GET)
    @ResponseBody
    public Result findMenu() {
        List<Menu> menuList = menuService.list(null);
        return Result.ok(menuList);
    }

    @ApiOperation(value = "根据id查询权限")
    @GetMapping("getMenu/{id}")
    public Result getMenu(@PathVariable Long id) {
        Menu menu = menuService.getById(id);
        return Result.ok(menu);
    }

    @ApiOperation(value = "新增权限")
    @RequestMapping(value = "saveMenu", method = RequestMethod.POST)
    @ResponseBody
    public Result saveMenu(@RequestBody Menu menu) {
        boolean save = menuService.save(menu);
        return save ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "逻辑删除权限")
    @DeleteMapping("{id}")
    public Result removeMenu(@PathVariable Long id) {
        boolean flag = menuService.removeById(id);
        return flag ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "修改权限")
    @PostMapping("updateMenu")
    public Result updateMenu(@RequestBody Menu menu) {
        boolean flag = menuService.updateById(menu);
        return flag ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "批量删除权限")
    @DeleteMapping("batchRemoveMenu")
    public Result batchRemoveMenu(@RequestBody List<Long> idList) {
        menuService.removeByIds(idList);
        return Result.ok();
    }

    @ApiOperation(value = "权限条件查询带分页")
    @PostMapping("findPageMenu/{current}/{limit}")
    public Result findPageMenu(@PathVariable long current,
                               @PathVariable long limit,
                               @RequestBody(required = false) MenuVo menuVo) {

        //创建page对象，传递当前页，每页记录数
        Page<Menu> page = new Page<>(current, limit);

        //构建条件
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        Integer id = menuVo.getId();
        String name = menuVo.getName();
        //条件可能为空
        if (!StringUtils.isEmpty(id)) {
            wrapper.eq("id", menuVo.getId());
        }
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", menuVo.getName());
        }
        //调用方法实现分页查询
        Page<Menu> pageDept = menuService.page(page, wrapper);
        return Result.ok(pageDept);
    }
}

